/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.evenmo.string;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;

/**
 * This is Description ExecutorService和Future示例
 * 获取质数
 * @author moyongjun
 * @date 2020/01/05
 */
public class Thread_Future {

    @Test
    public void test(){

        int i = new Random().nextInt(10);
        System.out.println(i);
        int i1 = new Random().nextInt();
        System.out.println(i1);


    }


    public static void main(String[] args) {



        long start = System.currentTimeMillis();
        try {
            List<Integer> call = getPrime(1,200000);
            System.out.println(call);
            System.out.println(call.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end=System.currentTimeMillis();


        System.out.println("耗时："+(end-start)+"毫秒");

start=System.currentTimeMillis();


        ForkJoinPool forkJoinPool = new ForkJoinPool();


        ExecutorService executorService = Executors.newFixedThreadPool(4);

        Future<List<Integer>> f1 = executorService.submit(new MyTask(1, 80000));

        Future<List<Integer>> f2 = executorService.submit(new MyTask(80001, 130000));
        Future<List<Integer>> f3 = executorService.submit(new MyTask(130001,170000));
        Future<List<Integer>> f4 = executorService.submit(new MyTask(170001,200000));

        try {
            List<Integer> p1 = f1.get();
            List<Integer> p2 = f2.get();
            List<Integer> p3 = f3.get();
            List<Integer> p4 = f4.get();

            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);
            System.out.println(p4);
        }catch (Exception e){
            e.printStackTrace();
        }

    end=System.currentTimeMillis();

        System.out.println("完成全部分割任务总耗时："+(end-start)+"毫秒");

        executorService.shutdown();


    }

    static class MyTask implements Callable<List<Integer>> {

        private int startPos;
        private int endPos;

        MyTask(int startPos,int endPos){
           this.startPos=startPos;
           this.endPos=endPos;
        }

        @Override
        public List<Integer> call() throws Exception {


            return getPrime(startPos,endPos);
        }


    }

    static boolean isPrime(int num){

        for (int i = 2; i < num/2; i++) {
            if(num%i==0){return false;}

        }
        return true;
    }

    static List<Integer> getPrime(int startPos,int endPos){
        List<Integer> primes= new ArrayList<>();

        for (int i = startPos; i <=endPos; i++) {
                if (isPrime(i)){
                    primes.add(i);
                }
        }
        return  primes;
    }
}
